/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.InspectionRecord;
import com.zhuoyue.elmt.service.InspectionRecordService;

/**
 * 电梯检验记录Controller
 * @author chenhao
 * @version 2016-09-09
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/inspectionRecord")
public class InspectionRecordController extends BaseController {

	@Autowired
	private InspectionRecordService inspectionRecordService;
	
	@ModelAttribute
	public InspectionRecord get(@RequestParam(required=false) String id) {
		InspectionRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = inspectionRecordService.get(id);
		}
		if (entity == null){
			entity = new InspectionRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:inspectionRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(InspectionRecord inspectionRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InspectionRecord> page = inspectionRecordService.findPage(new Page<InspectionRecord>(request, response), inspectionRecord); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/inspectionRecordList";
	}

	@RequiresPermissions("elmt:inspectionRecord:view")
	@RequestMapping(value = "form")
	public String form(InspectionRecord inspectionRecord, Model model) {
		model.addAttribute("inspectionRecord", inspectionRecord);
		return "zhuoyue/elmt/inspectionRecordForm";
	}

	@RequiresPermissions("elmt:inspectionRecord:edit")
	@RequestMapping(value = "save")
	public String save(InspectionRecord inspectionRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, inspectionRecord)){
			return form(inspectionRecord, model);
		}
		inspectionRecordService.save(inspectionRecord);
		addMessage(redirectAttributes, "保存电梯检验记录成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/inspectionRecord/?repage";
	}
	
	@RequiresPermissions("elmt:inspectionRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(InspectionRecord inspectionRecord, RedirectAttributes redirectAttributes) {
		inspectionRecordService.delete(inspectionRecord);
		addMessage(redirectAttributes, "删除电梯检验记录成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/inspectionRecord/?repage";
	}

}